﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using jsclr.Models;

namespace jsclr.Controllers
{
    [Authorize]
    public class ProgramsController : Controller
    {
        ILPrograms ilPrograms = new ILPrograms();
        
        public ActionResult Index()
        {
            string userId = this.getUserId();

            var userPrograms = from p in ilPrograms.Programs
                               where p.OwnerId == userId
                               select p; 
        

            return View(userPrograms.ToList());
        }

        public ActionResult Edit(int id)
        {
            if (id < 0)
                return View(new Program());
            
            var program = getProgram(id);

            if (program == null)
                return View("Error");

            return View(program);
        }

        [HttpPost]
        public ActionResult Edit(Program program)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(program);

                var existingProgram = getProgram(program.ProgramId);

                if (existingProgram == null)
                {
                    program.OwnerId = getUserId();

                    ilPrograms.Programs.Add(program);
                    ilPrograms.SaveChanges();
                }
                else
                {
                    ilPrograms.Entry(existingProgram).CurrentValues.SetValues(program);
                    ilPrograms.SaveChanges();
                }

                return RedirectToAction("Edit", new { id = program.ProgramId });
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            var program = getProgram(id);
            if (program == null)
                return View("Error");

            return View(program);
        }

        [HttpPost]
        public ActionResult Delete(Program program)
        {
            try
            {
                ilPrograms.Entry(program).State = EntityState.Deleted;
                ilPrograms.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private Program getProgram(int programId)
        {
            string userId = this.getUserId();

            return (from p in ilPrograms.Programs
                    where p.ProgramId == programId
                    && p.OwnerId == userId
                    select p).FirstOrDefault();   
        }

        private string getUserId()
        {
            var user = Membership.GetUser(User.Identity.Name);
            return (user != null && user.ProviderUserKey != null) ? user.ProviderUserKey.ToString() : string.Empty;
        }
    }
}
